package br.com.fiap.goaround.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;

import br.com.fiap.goaround.to.TrafficTO;

public class GenerateTrafficBase {

	// Lista aonde sera armazenado os enderecos com transito bom
	private Collection<TrafficTO> goodList = new ArrayList<TrafficTO>();
	// Lista aonde sera armazenado os enderecos com transito ruim
	private Collection<TrafficTO> badList = new ArrayList<TrafficTO>();

	
	public GenerateTrafficBase() throws IOException{
		
		String sourceLine = "";

     	//String arq_externo = "C:\\Users\\Douglas\\Workspace\\GoAroundWeb\\war\\Files\\links_maps.txt";
     	String arq_externo = "http://go-around.appspot.com/Files/links_maps.txt";
     	URL address = new URL(arq_externo.toString());
        // *** new version
     	Object urlContent = address.getContent();

		// Open the address and create a BufferedReader with the source code.
     	// *** old version
		//InputStreamReader pageInput = new InputStreamReader(address.openStream(), "UTF-8");
		InputStreamReader pageInput = new InputStreamReader((InputStream)urlContent , "UTF-8");
		BufferedReader source = new BufferedReader(pageInput);
		
        // Instanciando classe de leitura de HTML
		HtmlContentParser hcp = new HtmlContentParser();
		
		// Variavel onde armazenara a linha lida no arquivo
		
		// Ler arquivo enquanto a linha for diferente de null (Fim)
		while ((sourceLine = source.readLine()) != null) {
			if (sourceLine.matches(".*Sao-Paulo-SP.htm.*"))
				hcp.htmlSPReader(sourceLine);
			else
				hcp.htmlStreetReader(sourceLine);
		}

		pageInput.close();
		source.close();
		
		// Carrega Collection para disponibilizar para outras classes
		goodList = hcp.getEndboa();
		badList = hcp.getEndruim();
		
	}

	public Collection<TrafficTO> getGoodList() {
		return goodList;
	}

	public Collection<TrafficTO> getBadList() {
		return badList;
	}

}
